<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="任务编号" prop="taskCode">
        <el-input
          v-model="queryParams.taskCode"
          placeholder="请输入任务编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务名称" prop="taskName">
        <el-input
          v-model="queryParams.taskName"
          placeholder="请输入任务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设施类型" prop="deviceClassification">
        <el-select
          v-model="queryParams.deviceClassification"
          placeholder="请选择设施类型"
          clearable
        >
          <el-option
            v-for="dict in dict.type.task_device_classification"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="设备类型" prop="deviceType">
        <el-select
          v-model="queryParams.deviceType"
          placeholder="请选择设备类型"
          clearable
        >
          <el-option
            v-for="dict in dict.type.task_device_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="小区" prop="neighborhoodId">
        <el-select
          v-model="queryParams.neighborhoodId"
          placeholder="请选择小区"
          clearable
        >
          <el-option
            v-for="dict in dict.type.bus_neighborhood"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="所属街道" prop="streetId">
        <el-select
          v-model="queryParams.streetId"
          placeholder="请选择街道"
          clearable
        >
          <el-option
            v-for="dict in dict.type.bus_neighborhoodledger"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="班组" prop="teamId">
        <el-select
          v-model="queryParams.teamId"
          placeholder="请选择班组ID"
          clearable
        >
          <el-option
            v-for="dict in dict.type.bus_peoson_group"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="负责人员" prop="personName">
        <el-select
          v-model="queryParams.personName"
          placeholder="请选择负责人员"
          clearable
        >
          <el-option
            v-for="dict in dict.type.bus_person_name"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="状态" prop="taskStatus">
        <el-select
          v-model="queryParams.taskStatus"
          placeholder="请选择状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.bus_maintenance_task_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:maintenance:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:maintenance:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:maintenance:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:maintenance:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
        :columns="columns"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="maintenanceList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="序号" type="index" width="100" align="center" v-if="getColumn('index')">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
</el-table-column> -->
      <!-- <el-table-column label="主键" align="center" prop="id" v-if="getColumn('id')" /> -->
      <el-table-column
        label="任务编号"
        align="center"
        prop="taskCode"
        v-if="getColumn('taskCode')"
      />
      <el-table-column
        label="任务名称"
        align="center"
        prop="taskName"
        v-if="getColumn('taskName')"
      />
      <el-table-column
        label="设备编号"
        align="center"
        prop="deviceCode"
        v-if="getColumn('deviceCode')"
      />
      <el-table-column
        label="设施类型"
        align="center"
        prop="deviceClassification"
        v-if="getColumn('deviceClassification')"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.task_device_classification"
            :value="scope.row.deviceClassification"
          />
        </template>
      </el-table-column>

      <el-table-column
        label="设备类型"
        align="center"
        prop="deviceType"
        v-if="getColumn('deviceType')"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.task_device_type"
            :value="scope.row.deviceType"
          />
        </template>
      </el-table-column>

      <el-table-column
        label="所属街道"
        align="center"
        prop="streetId"
        v-if="getColumn('streetId')"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.bus_neighborhoodledger"
            :value="scope.row.streetId"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="所属小区"
        align="center"
        prop="neighborhoodId"
        v-if="getColumn('neighborhoodId')"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.bus_neighborhood"
            :value="scope.row.neighborhoodId"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="详细地址"
        align="center"
        prop="address"
        v-if="getColumn('address')"
      />
      <el-table-column
        label="所属班组"
        align="center"
        prop="teamId"
        v-if="getColumn('teamId')"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.bus_peoson_group"
            :value="scope.row.teamId"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="人员"
        align="center"
        prop="personId"
        v-if="getColumn('personId')"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.bus_person_name"
            :value="scope.row.personId"
          />
        </template>
      </el-table-column>

      <el-table-column
        label="状态"
        align="center"
        prop="taskStatus"
        v-if="getColumn('taskStatus')"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.bus_maintenance_task_status"
            :value="scope.row.taskStatus"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="数量"
        align="center"
        prop="taskStatusNumber"
        v-if="getColumn('taskStatusNumber')"
      />
      <el-table-column
        label="维保图片"
        align="center"
        prop="taskImages"
        v-if="getColumn('taskImages')"
      />
      <el-table-column
        label="备注"
        align="center"
        prop="taskRemarks"
        v-if="getColumn('taskRemarks')"
      />
      <el-table-column
        label="维保时间"
        align="center"
        prop="completeTime"
        width="180"
        v-if="getColumn('completeTime')"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.completeTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        v-if="getColumn('operate')"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:maintenance:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:maintenance:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :pageSizes="pageSizes"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改维保任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="任务名称" prop="taskName">
          <el-input v-model="form.taskName" placeholder="请输入任务名称" />
        </el-form-item>

        <el-form-item label="设备类型" prop="deviceType">
          <el-select v-model="form.deviceType" placeholder="请选择设备类型">
            <el-option
              v-for="dict in dict.type.task_device_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设施类型" prop="deviceClassification">
          <el-select
            v-model="form.deviceClassification"
            placeholder="请选择设施类型"
          >
            <el-option
              v-for="dict in dict.type.task_device_classification"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="taskStatus">
          <el-select
            v-model="form.taskStatus"
            placeholder="请选择状态"
            clearable
          >
            <el-option
              v-for="dict in dict.type.bus_maintenance_task_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="所属街道" prop="streetId">
          <el-select v-model="form.streetId" placeholder="请选择所属街道">
            <el-option
              v-for="dict in dict.type.bus_neighborhoodledger"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属小区" prop="neighborhoodId">
          <el-select v-model="form.neighborhoodId" placeholder="请选择小区">
            <el-option
              v-for="dict in dict.type.bus_neighborhood"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMaintenance,
  getMaintenance,
  delMaintenance,
  addMaintenance,
  updateMaintenance,
} from "@/api/business/maintenance";

export default {
  name: "Maintenance",
  dicts: [
    "bus_neighborhood",
    "bus_person_name",
    "bus_maintenance_task_status",
    "bus_peoson_group",
    "task_device_classification",
    "task_device_type",
    "bus_neighborhoodledger",
    "bus_peoson_group",
  ],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 维保任务表格数据
      maintenanceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceType: null,
        streetName: null,
        personName: null,
        taskCode: null,
        taskName: null,
        deviceClassification: null,
        taskStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        taskName: [
          { required: true, message: "任务名称不能为空", trigger: "blur" },
        ],
      },
      // 列信息
      columns: [
        { key: "index", label: `序号`, visible: true },
        { key: "id", label: "主键", visible: true },
        { key: "index", label: `序号`, visible: true },
        { key: "id", label: "主键", visible: true },
        { key: "deviceCode", label: "设备编号", visible: true },
        { key: "deviceType", label: "设备类型", visible: true },
        { key: "address", label: "详细地址", visible: true },
        { key: "streetId", label: "街道ID", visible: true },
        { key: "neighborhoodId", label: "小区ID", visible: true },
        { key: "personId", label: "人员ID", visible: true },
        { key: "taskCode", label: "任务编号", visible: true },
        { key: "taskName", label: "任务名称", visible: true },
        { key: "deviceClassification", label: "设施类型", visible: true },
        { key: "taskStatus", label: "状态", visible: true },
        { key: "taskStatusNumber", label: "数量", visible: true },
        { key: "taskImages", label: "维保图片", visible: true },
        { key: "taskRemarks", label: "备注", visible: true },
        { key: "completeTime", label: "维保时间", visible: true },
        { key: "operate", label: `操作`, visible: true },
      ],
      // 分页参数
      pageSizes: [10, 50, 100, 500, 1000],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询维保任务列表 */
    getList() {
      this.loading = true;
      listMaintenance(this.queryParams).then((response) => {
        this.maintenanceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        delFlag: null,
        createUser: null,
        createBy: null,
        createTime: null,
        updateUser: null,
        updateBy: null,
        updateTime: null,
        taskUniCode: null,
        deviceCode: null,
        deviceType: null,
        deviceTypeLabel: null,
        address: null,
        streetId: null,
        streetName: null,
        neighborhoodId: null,
        neighborhoodName: null,
        teamId: null,
        teamName: null,
        personId: null,
        personName: null,
        taskCode: null,
        taskName: null,
        deviceId: null,
        deviceClassification: null,
        deviceClassificationLabel: null,
        taskStatus: null,
        taskStatusNumber: null,
        taskImages: null,
        taskRemarks: null,
        completeTime: null,
        completePersonId: null,
        confirmStatus: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加维保任务";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getMaintenance(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改维保任务";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateMaintenance(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            console.log("维保设备");
            console.log(this.form);
            addMaintenance(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除选中的数据项？")
        .then(function () {
          return delMaintenance(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "business/maintenance/export",
        {
          ...this.queryParams,
        },
        `maintenance_${new Date().getTime()}.xlsx`
      );
    },
    /** 列是否显示 */
    getColumn(key) {
      const res = this.columns.filter((item) => item.key === key);
      if (res.length >= 1) {
        return res[0].visible;
      } else {
        return false;
      }
    },
  },
};
</script>
